package com.meiqiongproperty.yiguanjia.fragment.order;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OrderLifeServiceDetailLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendOrderDetailList;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendShopOrderPay;
import com.meiqiongproperty.yiguanjia.httpbean.order.LifeServiceOrderDetailBean;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderRepayWXBean;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderRepayZFBBean;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/6/20.
 * package:com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter
 */

public class OrderLifeServiceDetailPayFragment extends BaseFragment {
    OrderLifeServiceDetailLayoutBinding binding;
    String orderId = "";
    String perPrice = "";
    String amount = "";
    String serviceTime = "";
    String paystatus = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.order_life_service_detail_layout, container, false);
        binding.setClick(this);

        binding.navigation.setTitle("订单详情");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });

        savedInstanceState = getArguments();
        if (savedInstanceState != null) {
            orderId = savedInstanceState.getString("orderId");
            perPrice = savedInstanceState.getString("price");
            amount = savedInstanceState.getString("amount");
            serviceTime = savedInstanceState.getString("serviceTime");
        }
        getLifeServiceDetail();
        return binding.getRoot();
    }

    /**
     * 生活服务订单详情
     */
    public void getLifeServiceDetail() {
        SendOrderDetailList send = new SendOrderDetailList();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setOrderId(orderId);
        send.setOrderType("2");

        ApiService.getLifeServiceOrderDetail(getActivity(), new IHttpCallback<LifeServiceOrderDetailBean>() {
            @Override
            public void onNext(LifeServiceOrderDetailBean lifeServiceOrderDetailBean) {
                Gson gson = new Gson();
                LogUtil.d("lifeServiceOrderDetailBean===========>" + gson.toJson(lifeServiceOrderDetailBean));
                binding.orderNumber.setText("订单号：" + lifeServiceOrderDetailBean.getOrderdetail().getOrderId());
                binding.serviceProjectName.setText(lifeServiceOrderDetailBean.getOrderdetail().getOrderTitle());
                binding.serviceAddress.setText("服务地址：" + lifeServiceOrderDetailBean.getOrderdetail().getAddress());
                binding.servicePerPrice.setText("服务单价：" + perPrice + "元/小时");
                binding.serviceNumber.setText("服务数量：" + amount);
                binding.serviceTime.setText("服务时间：" + serviceTime);
                binding.orderTime.setText("下单时间：" + lifeServiceOrderDetailBean.getOrderdetail().getCreateAt());
                binding.orderLifeServicePayType.setText("支付方式：" + lifeServiceOrderDetailBean.getOrderdetail().getPayMethodName() + "(" + lifeServiceOrderDetailBean.getOrderdetail().getPayStatusName() + ")");
                binding.orderServiceTotalPrice.setText("￥" + lifeServiceOrderDetailBean.getOrderdetail().getOrderPrice());
                paystatus = lifeServiceOrderDetailBean.getOrderdetail().getPayStatus();
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }

    /**
     * 服务订单支付
     */
    public void orderServicePay() {
        SendShopOrderPay send = new SendShopOrderPay();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setOrderId(orderId);
        send.setOrderType("2");

        switch (paystatus) {
            case "1":
                ApiService.OrderShopZFBPay(getActivity(), new IHttpCallback<ShopOrderRepayZFBBean>() {
                    @Override
                    public void onNext(ShopOrderRepayZFBBean shopOrderRepayZFBBean) {
                        LogUtil.d("wxdfdfddfdfd====>支付宝调用成功");
                        String orderInfo = shopOrderRepayZFBBean.getParameter().getParameter();
                        doAlipay(orderInfo);
                    }

                    @Override
                    public void onError(int code, String message) {

                    }
                }, Utils.getRequestBody(send));
                break;
            case "2":
                ApiService.OrderShopWXPay(getActivity(), new IHttpCallback<ShopOrderRepayWXBean>() {
                    @Override
                    public void onNext(ShopOrderRepayWXBean shopOrderRepayWXBean) {
                        LogUtil.d("wxdfdfddfdfd====>微信调用成功");
                        doShopOrderWXPay(shopOrderRepayWXBean.getParameter().getParameter(), shopOrderRepayWXBean.getParameter().getParameter().getAppid());
                    }

                    @Override
                    public void onError(int code, String message) {
                        LogUtil.d("wxdfdfddfdfd====>微信调用失败" + message);
                    }
                }, Utils.getRequestBody(send));
                break;
        }
    }

    /**
     * 确认支付
     */
    public void payServiceOrderClick(View view) {
        if (Utils.isFastDoubleClick()) return;
        orderServicePay();
    }
}

